package com.fcat.springboot.mybatis.service.impl;

import com.fcat.springboot.mybatis.mapper.base.TsuantoneBaseMapper;
import com.fcat.springboot.mybatis.service.TsuantoneBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @BelongsProject: fcat-base
 * @BelongsPackage: com.fcat.springboot.mybatis.service.impl
 * @Author: susen
 * @CreateTime: 2019-09-16 17:17
 * @Description:
 */
public class TsuantoneBaseServiceImpl<M extends TsuantoneBaseMapper<T>, T> implements TsuantoneBaseService<T> {
    @Autowired
    protected M mapper;

    @Override
    public T selectOne(T entity) {
        return mapper.selectOne(entity);
    }

    @Override
    public T selectById(Object id) {
        return mapper.selectByPrimaryKey(id);
    }


    @Override
    public List<T> selectList(T entity) {
        return mapper.select(entity);
    }

    @Override
    public List<T> selectByExample(Example example) {
        return mapper.selectByExample(example);
    }

    @Override
    public T selectOneByExample(Example example) {
        return mapper.selectOneByExample(example);
    }

    @Override
    public Integer selectCountByExample(Example example) {
        return mapper.selectCountByExample(example);
    }


    @Override
    public List<T> selectListAll() {
        return mapper.selectAll();
    }


    @Override
    public Long selectCount(T entity) {
        return Long.valueOf(mapper.selectCount(entity));
    }

    @Override
    public int insert(T entity) {
        return mapper.insert(entity);
    }

    @Override
    public int insertSelective(T entity) {
        return mapper.insertSelective(entity);
    }

    @Override
    public int delete(T entity) {
        return mapper.delete(entity);
    }

    @Override
    public int deleteById(Object id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateById(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    @Override
    public int updateSelectiveById(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int batchUpdateById(List<T> list) {
        if (list == null || list.size() == 0) return 0;
        return mapper.updateListById(list);
    }

    @Override
    public int batchInserts(List<T> list) {
        if (list == null || list.size() == 0) return 0;
        return mapper.insertList(list);
    }

    @Override
    public int batchDeleteById(List list) {
        if (list == null || list.size() == 0) return 0;
        return deleteByIds(list);
    }

    @Override
    public int insertList(List list) {
        if (list == null || list.size() == 0) return 0;
        return mapper.insertList(list);
    }

    @Override
    public int deleteByIds(List list) {
        if (list == null || list.size() == 0) return 0;
        String ids = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == list.size() - 1) {
                ids = ids + list.get(i);
            } else {
                ids = ids + list.get(i) + ",";
            }
        }
        return mapper.deleteByIds(ids);
    }

    @Override
    public int deleteByIds(String ids) {
        if (StringUtils.isEmpty(ids)) return 0;
        return mapper.deleteByIds(ids);
    }
}
